package com.turing.usdtpay.mvp.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.Window;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.LinearLayout;

import com.turing.usdtpay.R;
import com.turing.usdtpay.common.StateContants;
import com.turing.usdtpay.entity.UserData;
import com.turing.usdtpay.manager.UserManager;
import com.turing.usdtpay.mvp.contract.LoginContract;
import com.turing.usdtpay.mvp.presenter.LoginPresenter;
import com.turing.usdtpay.utils.LogUtils;
import com.turing.usdtpay.utils.Utils;

/**
 * @作者：JAY
 * @创建时间：2019/10/16 9:57
 * @描述：
 **/
public class SplashActivity extends AppCompatActivity implements LoginContract.View {
    public static final String GLOBAL_TAG = "turing";
    private LoginContract.Present mPresent;
    private boolean needLogin = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 设置没有标题栏
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_splash);
        utilsInit(this);
        LinearLayout layoutSplash=(LinearLayout) findViewById(R.id.splash_layout);
        initData();
        initAnima(layoutSplash);

    }

    /**
     * 初始化工具类
     *
     * @param activity 上下文环境
     */
    private void utilsInit(Activity activity) {
        Utils.init(activity);
        LogUtils.Builder builder = new LogUtils.Builder().setLogSwitch(true)
                .setGlobalTag(GLOBAL_TAG)
                .setLog2FileSwitch(false)
                .setLogHeadSwitch(true)
                .setBorderSwitch(true);
        LogUtils.d(builder.toString());
    }

    private void initData() {
        StateContants.CONTEXT = this.getApplicationContext();
        setPresenter(new LoginPresenter(this));
        String lastUser = UserManager.getInstance().getLastUser();
        if (TextUtils.isEmpty(lastUser)){
            needLogin = true;
        }else {
            needLogin = false;
            UserData user = UserManager.getInstance().getCurrentAccount(lastUser);
            LogUtils.d("user -->"+user.toString());
            mPresent.login(user.getAccount(),user.getPwd());
        }
    }

    private void initAnima(LinearLayout layoutSplash) {
        AlphaAnimation alphaAnimation=new AlphaAnimation(0.1f,1.0f);
        alphaAnimation.setDuration(3000);//设置动画播放时长1000毫秒（1秒）
        layoutSplash.startAnimation(alphaAnimation);
        //设置动画监听
        alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
            }
            //动画结束
            @Override
            public void onAnimationEnd(Animation animation) {
                if (needLogin){
                    //页面的跳转
                    Intent intent=new Intent(SplashActivity.this,LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
            }
            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });
    }

    @Override
    public void loginSuccess() {
        startActivity(new Intent(SplashActivity.this,RodOrderActivity.class));
        finish();
    }

    @Override
    public void loginFail(String msg) {
        needLogin = true;
        //页面的跳转
        Intent intent=new Intent(SplashActivity.this,LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void setPresenter(LoginContract.Present presenter) {
        mPresent = presenter;
    }
}
